package android.demo.service.binder;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.demo.BaseActivity;
import android.demo.service.binder.LocalService.LocalBinder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class BindingActivity extends BaseActivity {
	
	private LocalService localService;  
	private boolean bound;

	@Override  
	protected void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);
		
		//布局
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		//btnLocal
		Button btnStartLocal = new Button(this);
		btnStartLocal.setText("启动本地服务");
		btnStartLocal.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				startService(new Intent(BindingActivity.this, LocalService.class));
			}
		});
		layout.addView(btnStartLocal);
		
		//btnStopLocal
		Button btnStopLocal = new Button(this);
		btnStopLocal.setText("停止本地服务");
		btnStopLocal.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				stopService(new Intent(BindingActivity.this, LocalService.class));
			}
		});
		layout.addView(btnStopLocal);
		
		//btnBindLocal
		Button btnBindLocal = new Button(this);
		btnBindLocal.setText("绑定本地服务");
		btnBindLocal.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(BindingActivity.this, LocalService.class); 
				bindService(intent, localConnection, Service.BIND_AUTO_CREATE);
			}
		});
		layout.addView(btnBindLocal);

		//btnUnbindLocal
		Button btnUnbindLocal = new Button(this);
		btnUnbindLocal.setText("解除绑定本地服务");
		btnUnbindLocal.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				if (bound) {  
					unbindService(localConnection);  
					bound = false;  
				} 
			}
		});
		layout.addView(btnUnbindLocal);
		
		//btnTest
		Button btnTest = new Button(this);
		btnTest.setText("方法测试");
		btnTest.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				onButtonClick(v);
			}
		});
		layout.addView(btnTest);
		
		setContentView(layout);
	}  

	 /** 用户点击button，就读取Service里的随机数 */  
	public void onButtonClick(View v) {  
		if (bound) {  
			// 用Service的对象，去读取随机数  
			String msg = localService.testMethod();  
			Toast.makeText(this, "msg: " + msg, Toast.LENGTH_SHORT).show();  
		}  
	}  

	private ServiceConnection localConnection = new ServiceConnection() {
		
		public void onServiceDisconnected(ComponentName name) {
			Log.i(TAG, "LocalService disconnected!");
			bound = false;  
		}
		
		synchronized public void onServiceConnected(ComponentName name, IBinder service) {
			Log.i(TAG, "LocalService connected.");
			// 已经绑定了LocalService，强转IBinder对象，调用方法得到LocalService对象  
			LocalBinder binder = (LocalBinder) service;            
			localService = binder.getService();            
			bound = true;  
		}
	};
	
	@Override  
	protected void onStart() {  
		super.onStart();  
		// 绑定Service，绑定后就会调用mConnetion里的onServiceConnected方法  
		Intent intent = new Intent(this, LocalService.class);  
		bindService(intent, localConnection, Context.BIND_AUTO_CREATE);  
	}  

	@Override  
	protected void onStop() {  
		super.onStop();  
		// 解绑Service，这样可以节约内存  
		if (bound) {  
			unbindService(localConnection);  
			bound = false;  
		}  
	} 

}
